package com.ray.service;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qq.weixin.mp.aes.AesException;
import com.qq.weixin.mp.aes.WXBizMsgCrypt;
import com.ray.util.WeiXinParamesUtil;

public class CoreService extends HttpServlet {
	 private static final long serialVersionUID = 4440739483644821986L;  
	    
	      /** 
	      29 * 确认请求来自微信服务器 
	      30  */  
	 
	 
	    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      
	          System.out.println("微信回调");
	          // 微信加密签名  
	              String msg_signature = request.getParameter("msg_signature");  
	             // 时间戳  
	              String timestamp = request.getParameter("timestamp");  
	             // 随机数  
	              String nonce = request.getParameter("nonce");  
	              // 随机字符串  
	              String echostr = request.getParameter("echostr");  
	       
	             System.out.println("request=" + request.getRequestURL());  
	        
	              PrintWriter out = response.getWriter();  
	              // 通过检验msg_signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败  
	              String result = null;  
	              try {
	                  WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeiXinParamesUtil.token, WeiXinParamesUtil.encodingAESKey, WeiXinParamesUtil.corpID);  
	                  result = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr);  
	              } catch (AesException e) {  
	                  e.printStackTrace();  
	              }  
	              if (result == null) {  
	                  result = WeiXinParamesUtil.token;  
	              }  
	              out.print(result);  
	              out.close();  
	              out = null;  
	      }    
}
